import pandas as pd
import numpy as np
from faker import Faker
from datetime import datetime, timedelta
import random

# 初始化工具
fake = Faker()
np.random.seed(42)
random.seed(42)

# 1. 基础配置
positions = ['GK', 'CB', 'LB', 'RB', 'CDM', 'CM', 'CAM', 'LM', 'RM', 'LW', 'RW', 'ST']
nations = ['England', 'Brazil', 'France', 'Germany', 'Spain', 'Argentina',
           'Portugal', 'Netherlands', 'Belgium', 'Italy', 'Croatia', 'Senegal']
clubs = ['Man City', 'Liverpool', 'Bayern', 'PSG', 'Real Madrid', 'Barcelona',
         'Chelsea', 'Juventus', 'AC Milan', 'Arsenal', 'Dortmund', 'Atletico Madrid']

# 生成10000名主球运动员数据
# 2. 生成函数
def generate_player(id):
    age = random.randint(16, 40)
    birth_date = datetime.now() - timedelta(days=age * 365 + random.randint(0, 364))
    height = random.randint(160, 210)
    weight = random.randint(60, 100)
    position = random.choice(positions)

    # 根据位置生成能力属性
    if position == 'GK':
        gk_attrs = np.random.randint(70, 95, size=5)
        field_attrs = np.random.randint(10, 50, size=40)
        attrs = np.concatenate([field_attrs[:34], gk_attrs, field_attrs[34:]])
    else:
        attrs = np.random.randint(30, 99, size=45)
        attrs[-5:] = np.random.randint(10, 30, size=5)  # GK属性低

    return {
        'Name': fake.name(),
        'Nationality': random.choice(nations),
        'National_Position': position if random.random() > 0.7 else np.nan,
        'National_Kit': random.randint(1, 23) if pd.notna(position) else np.nan,
        'Club': random.choice(clubs),
        'Club_Position': position,
        'Club_Kit': random.randint(1, 99),
        'Club_Joining': (datetime.now() - timedelta(days=random.randint(30, 365 * 5))).strftime('%Y-%m-%d'),
        'Contract_Expiry': (datetime.now() + timedelta(days=random.randint(365, 365 * 5))).strftime('%Y-%m-%d'),
        'Rating': random.randint(65, 95),
        'Height': height,
        'Weight': weight,
        'Preffered_Foot': random.choice(['Left', 'Right']),
        'Birth_Date': birth_date.strftime('%Y-%m-%d'),
        'Age': age,
        'Preffered_Position': position,
        'Work_Rate': random.choice(['Low/Low', 'Low/Medium', 'Low/High',
                                    'Medium/Low', 'Medium/Medium', 'Medium/High',
                                    'High/Low', 'High/Medium', 'High/High']),
        'Weak_foot': random.randint(1, 5),
        'Skill_Moves': random.randint(1, 5),
        'Ball_Control': attrs[0],
        'Dribbling': attrs[1],
        'Marking': attrs[2],
        'Sliding_Tackle': attrs[3],
        'Standing_Tackle': attrs[4],
        'Aggression': attrs[5],
        'Reactions': attrs[6],
        'Attacking_Position': attrs[7],
        'Interceptions': attrs[8],
        'Vision': attrs[9],
        'Composure': attrs[10],
        'Crossing': attrs[11],
        'Short_Pass': attrs[12],
        'Long_Pass': attrs[13],
        'Speed': attrs[14],
        'Stamina': attrs[15],
        'Strength': attrs[16],
        'Balance': attrs[17],
        'Agility': attrs[18],
        'Jumping': attrs[19],
        'Heading': attrs[20],
        'Shot_Power': attrs[21],
        'Finishing': attrs[22],
        'Long_Shots': attrs[23],
        'Curve': attrs[24],
        'Freekick_Accuracy': attrs[25],
        'Penalties': attrs[26],
        'Volleys': attrs[27],
        'GK_Positioning': attrs[28],
        'GK_Diving': attrs[29],
        'GK_Kicking': attrs[30],
        'GK_Handling': attrs[31],
        'GK_Reflexes': attrs[32]
    }


# 3. 生成数据
data = [generate_player(i) for i in range(10000)]
df = pd.DataFrame(data)

# 4. 保存为CSV
df.to_csv('football_players_10k.csv', index=False)
print("数据集已生成：football_players_10k.csv")